//引入express模块
const express = require("express");
//引入数据库连接模块
const pool = require("../pool.js");
//创建路由器
const r = express.Router();

//第三方图形验证码模块:
//下载  npm i svg-captcha
const svgCaptcha = require("svg-captcha");

//根据uid查询用户信息
//接口的地址：http://127.0.0.1:3000/v1/users/search
// r.get("/search", (req, res) => {
//   var _uid = req.query.jid;
//   var sql = "select * from j_user where jid=?";
//   pool.query(sql, [_uid], (err, r) => {
//     if (err) throw err;
//     res.send({ msg: 200, data: r });
//   });
// });

//接口的地址：http://127.0.0.1:3000/v1/users/unames
//接口的请求方式:get
// 验证用户名是否存在
r.get("/unames", (req, res, next) => {
  //执行SQL语句
  var obj = req.query;
  // console.log(obj)
  pool.query(
    "select uname from j_user where uname=?",
    [obj.uname],
    (err, r) => {
      // console.log(r);
      if (err) {
        next(err);
        return;
      }
      if (r.length > 0) {
        res.send({ code: 200, msg: "用户名已存在" });
        return;
      } else {
        res.send({ code: 301, msg: "欢迎使用" });
        return;
      }
    }
  );
});

//接口的地址：http://127.0.0.1:3000/v1/users/phone
//接口的请求方式:get
//验证手机号是否存在
r.get("/phone", (req, res, next) => {
  //执行SQL语句
  var obj = req.query.phone;
  pool.query("select phone from user where phone=?", [obj.phone], (err, r) => {
    if (err) {
      next(err);
    }
    //console.log(r)
    if (r.length > 0) {
      res.send({ code: 404, msg: "手机号已存在" });
      return;
    } else if (r.length === 0) {
      res.send({ code: 202, msg: "欢迎使用" });
    }
  });
});

//添加用户接口(post /add)
//接口的地址：http://127.0.0.1:3000/v1/users/add
r.post("/add", (req, res, next) => {
  var obj = req.body;
  pool.query("insert into user set?", [obj], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.affectedRows == 1) {
      res.send({ code: 200, msg: "添加成功" });
    } else {
      res.send({ code: 404, msg: "添加失败" });
    }
  });
});

//.根据uid删除用户
//接口的地址：http://127.0.0.1:3000/v1/users/delUser
r.delete("/delUser", (req, res) => {
  // console.log(req);
  var _jid = req.query.jid;
  var sql = "delete from j_user where jid=?";
  pool.query(sql, [_jid], (err, r) => {
    if (err) throw err;
    // console.log(r);
    if (r.affectedRows > 0) {
      res.send({ code: 200, msg: "删除成功" });
    } else {
      res.send({ code: 404, msg: "删除失败" });
    }
  });
});

//.根据uid修改用户
//接口的地址：http://127.0.0.1:3000/v1/users/putUser
r.put("/putUser", (req, res) => {
  // console.log(req);
  var _uname = req.body.uname;
  var _upwd = req.body.upwd;
  var _uphone = req.body.uphone;
  var _email = req.body.email;
  var _jid = req.body.jid;
  var sql = "update j_user set? where jid=?";
  pool.query(sql, [_jid], (err, r) => {
    if (err) throw err;
    // console.log(r);
    if (r.affectedRows > 0) {
      res.send({ code: 200, msg: "修改成功" });
    } else {
      res.send({ code: 404, msg: "修改失败" });
    }
  });
});
// 根据用户名删除用户信息
//接口的地址：http://127.0.0.1:3000/v1/users/juser
r.get("/juser", (req, res) => {
  var _uname = req.query.uname;
  // console.log(_uname);
  var sql = "select * from j_user where uname=?";
  pool.query(sql, [_uname], (err, r) => {
    if (err) throw err;
    res.send({ msg: 200, data: r });
  });
});
// 查询所有用户信息
//接口的地址：http://127.0.0.1:3000/v1/users/all
r.get("/all", (req, res) => {
  // console.log(_uname);
  var sql = "select * from j_user";
  pool.query(sql, (err, r) => {
    if (err) throw err;
    res.send({ msg: 200, data: r });
  });
});

// 管理员登录
//接口的地址：http://127.0.0.1:3000/v1/users/loginBase
r.post("/loginBase", (req, res) => {
  // console.log(req.body);
  let u_name = req.body.uname;
  let u_pwd = req.body.upwd;
  let Administrator = "1";
  // console.log(u_name, u_pwd);
  let sql =
    "select * from j_user where uname=? and upwd=MD5(?) and Administrator=?";
  pool.query(sql, [u_name, u_pwd, Administrator], (err, r) => {
    if (err) throw err;
    // console.log(r.length);
    if (r.length == 0) {
      res.send({ msg: 404, data: "登录失败" });
    } else {
      res.send({ msg: 200, data: "登录成功" });
    }
  });
});

module.exports = r;
